* @ingroup Database
*/
use Psr\Log\LoggerInterface;
+use Wikimedia\ScopedCallback;
/**
* Database connection, tracking, load balancing, and transaction manager for a cluster
$this->errorLogger = isset( $params['errorLogger'] )
? $params['errorLogger']
: function ( Exception $e ) {
- trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_WARNING );
+ trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
};
foreach ( [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ] as $key ) {
if ( $i == self::DB_REPLICA ) {
$this->mLastError = 'Unknown error'; // reset error string
# Try the general server pool if $groups are unavailable.
- $i = in_array( false, $groups, true )
+ $i = ( $groups === [ false ] )
? false // don't bother with this if that is what was tried above
: $this->getReaderIndex( false, $domain );
# Couldn't find a working server in getReaderIndex()?
} elseif ( isset( $this->mConns['local'][$i][0] ) ) {
$conn = $this->mConns['local'][$i][0];
} else {
+ if ( !isset( $this->mServers[$i] ) || !is_array( $this->mServers[$i] ) ) {
+ throw new InvalidArgumentException( "No server with index '$i'." );
+ }
+ // Open a new connection
$server = $this->mServers[$i];
$server['serverIndex'] = $i;
$conn = $this->reallyOpenConnection( $server, false );
": reusing free connection from $oldDomain for $domain" );
}
} else {
+ if ( !isset( $this->mServers[$i] ) || !is_array( $this->mServers[$i] ) ) {
+ throw new InvalidArgumentException( "No server with index '$i'." );
+ }
// Open a new connection
$server = $this->mServers[$i];
$server['serverIndex'] = $i;
* @throws DBAccessError
* @throws InvalidArgumentException
*/
- protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
+ protected function reallyOpenConnection( array $server, $dbNameOverride = false ) {
if ( $this->disabled ) {
throw new DBAccessError();
}
- if ( !is_array( $server ) ) {
- throw new InvalidArgumentException(
- 'You must update your load-balancing configuration. ' .
- 'See DefaultSettings.php entry for $wgDBservers.' );
- }
-
if ( $dbNameOverride !== false ) {
$server['dbname'] = $dbNameOverride;
}
// If all servers were busy, mLastError will contain something sensible
throw new DBConnectionError( null, $this->mLastError );
} else {
- $context['db_server'] = $conn->getProperty( 'mServer' );
+ $context['db_server'] = $conn->getServer();
$this->connLogger->warning(
"Connection error: {last_error} ({db_server})",
$context
$cache->makeGlobalKey( __CLASS__, 'server-read-only', $masterServer ),
self::TTL_CACHE_READONLY,
function () use ( $domain, $conn ) {
- $this->trxProfiler->setSilenced( true );
+ $old = $this->trxProfiler->setSilenced( true );
try {
$dbw = $conn ?: $this->getConnection( self::DB_MASTER, [], $domain );
$readOnly = (int)$dbw->serverIsReadOnly();
} catch ( DBError $e ) {
$readOnly = 0;
}
- $this->trxProfiler->setSilenced( false );
+ $this->trxProfiler->setSilenced( $old );
return $readOnly;
},
[ 'pcTTL' => $cache::TTL_PROC_LONG, 'busyValue' => 0 ]
* @return ScopedCallback|null
*/
final protected function getScopedPHPBehaviorForCommit() {
- if ( PHP_SAPI != 'cli' ) { // http://bugs.php.net/bug.php?id=47540
+ if ( PHP_SAPI != 'cli' ) { // https://bugs.php.net/bug.php?id=47540
$old = ignore_user_abort( true ); // avoid half-finished operations
return new ScopedCallback( function () use ( $old ) {
ignore_user_abort( $old );